/*
#========================================================================
#
# Alan Rogers
# Massachusetts Institute of Technology Haystack Observatory
# Program: vlbi2
# Calculate cross or autocorrelation spectra from Mk5b recorded data.
#
# The vlbi2 program is used to correlate two Mark 5B files with the same
# starting record time and record mode. A postscript file dd1.pos is
# produced showing the correlation amplitude and phase as a function
# of frequency in all channels in the two files with all channels
# plotted on the same axes. Correlation coefficients (fringe amplitudes,
# which will be 1.00 for an autocorrelation) are indicated below each
# channel. The vlbi2 program accepts two files, each 10 MB in size,
# and two flags. The usage is:
#
#        vlbi2 <file1> <file2> [-2bit <2bit_arg>] [-rev <rev_arg>]
#
# Where the items in [] are optional. The -2bit flag indicates whether
# the files are 2 bits/sample recordings (2bit_arg is 1) or 1 bit/sample
# (2bit_arg is 0) [default]. The files must be the same number of
# bits per sample. The .rev flag determines whether the channels in dd1.pos
# are plotted in reverse order (rev_arg is 1) or normal order
# (rev_arg is 0) [default]. It is important to note that with the .rev
# flag, though the channels can be plotted in the reverse order, they
# are not flipped in frequency. If file1 and file2 are the same, an
# autocorrelation is produced; if they are different a cross correlation
# is produced.
#
#
#=======================================================================
#
# Dec 2010
# S.Weston              stuart.weston@aut.ac.nz
# AUT University
#
# Modified to use DISLIN so that we can see the data better than just
# printed across the bottom of an A4 sheet in postscript. Also use
# DISLIN widgets to provide a better UI.
#
#=======================================================================
*/

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "vlbi2.h"

void print(int, int, char *, char *,int,int);

void print(int num, int chan,char *filename1,char *filename2,int rev,int tbit)
{
    int x,y,i,hr,mn,sec,iter,ichan;
    double integ,freq,temper,t,pdum,xx1,xx2,yy1,yy2,h,s,b,xoff,yoff;
    double secs,amp,phase,delay,delaymin,delaymax,elev,secss,seclast,secfirst,ampmax, secprev;
    double timediff, count,avamp,namp;
    time_t now;
    int timemode;
    FILE *file3;

       if ((file3 = fopen("dd1.pos", "w")) == NULL) {
            printf(" output error\n");
            return;
       }

        fprintf(file3, "%%!PS-Adobe-\n%c%cBoundingBox:  0 0 612 792\n%c%cEndProlog\n", '%', '%', '%', '%');
        fprintf(file3, "1 setlinewidth\n");
        fprintf(file3, "/Times-Roman findfont\n 8 scalefont\n setfont\n");
      xoff = 40; yoff = 100;

      xx1 = xoff; xx2=xx1+500.0;
      yy1 = yoff; yy2=yy1+300.0;
      h=s=b=0;
      fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx1, yy1, xx2, yy1, h, s, b);
      fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx1, yy1+150.0, xx2, yy1+150.0, h, s, b);
      fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx1, yy2, xx2, yy2, h, s, b);
      fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx1, yy1, xx1, yy2, h, s, b);
      fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx2, yy1, xx2, yy2, h, s, b);

	x = xoff; y = 100;
        fprintf(file3, "/Times-Roman findfont\n 10 scalefont\n setfont\n");
	for(iter=0;iter<2;iter++){
	for(ichan=0;ichan<chan;ichan++){
        namp=avamp=0.0;
	for(i=0;i<NSIZ2;i++){
                h=s=b=0;
	amp = sqrt(xspecr[ichan*NSIZ2+i]*xspecr[ichan*NSIZ2+i]
			+xspeca[ichan*NSIZ2+i]*xspeca[ichan*NSIZ2+i]);
	phase = atan2(xspeca[ichan*NSIZ2+i],xspecr[ichan*NSIZ2+i]);
	amp = amp/num;
        avamp += amp;
        namp++;
        xx2=x+(i+ichan*NSIZ2)*500.0/(NSIZ2*16.0);
        if(iter==0) yy2 = y+amp*75.0;
	else yy2 = phase * 75.0 / PI  + y + 150.0 + 75.0;
	if(iter == 0 && yy2 > y+150.0) yy2=y+150.0;
	if(i==0){xx1=xx2; yy1=yy2;}
	         h=s=b=0;
                  s=b=1;
                  h = ichan*0.67/chan;
                  if (h > 0.67) h = 0.67;
		  if (h < 0.0) h = 0.0;
		 if(iter) h=s=b=0;
	if(!iter || (iter && (i>10 && i<NSIZ2-10)&& fabs(yy2-yy1)<37.5))
          fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n %2.1f %2.1f %2.1f sethsbcolor stroke\n"
                    ,xx1, yy1, xx2, yy2, h, s, b);
                xx1 = xx2; yy1 = yy2;
		}
	xx1=xx2=x+(ichan*NSIZ2)*500.0/(NSIZ2*16.0);
	yy1=yoff; yy2=yoff+300.0;
	fprintf(file3, "0 0 0 sethsbcolor\n");
	fprintf(file3, "newpath\n %5.1f %5.1f moveto \n %5.1f %5.1f lineto\n 0 0 0 sethsbcolor stroke\n"
			,xx1, yy1, xx2, yy2);
	fprintf(file3,"%5.1f %5.1f moveto\n (%2d) show\n",xx1+10,yoff-10,ichan);
	fprintf(file3,"%5.1f %5.1f moveto\n (%5.2f) show\n",xx1+10,yoff-35,avamp/namp);
	       }
               }
        /* write bottom of page */
        x = 100; y = 60.0;
        now = time(NULL);
	fprintf(file3, "0 0 0 sethsbcolor\n");
        fprintf(file3, "/Times-Roman findfont\n 10 scalefont\n setfont\n");
	fprintf(file3,"100 50 moveto\n (file1: %s file2: %s bits/sam %d) show\n",filename1,filename2,tbit+1);
        if(!rev) fprintf(file3,"220 80 moveto\n (DBE channel number) show\n");
        else fprintf(file3,"220 80 moveto\n (DBE channel number reversed) show\n");
	fprintf(file3,"30 150 moveto\n 90 rotate\n (amplitude) show\n -90 rotate\n");
	fprintf(file3,"30 300 moveto\n 90 rotate\n (phase) show\n -90 rotate\n");
        fprintf(file3, "/Times-Roman findfont\n 8 scalefont\n setfont\n");
        fprintf(file3,"%d %d moveto\n (%s) show\n",x+20+300,20,ctime(&now));
        fprintf(file3, "showpage\n%c%cTrailer\n", '%', '%');
        fclose(file3);
}

